package com.jadcon.bedbreakfast.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.Customer;
import com.jadcon.bedbreakfast.domain.CustomerService;
import com.jadcon.bedbreakfast.domain.Reservation;
import com.jadcon.bedbreakfast.domain.ReservationService;

@Controller
public class ViewReservationController {

	@Autowired
	private ReservationService reservationService;
	@Autowired
	private CustomerService customerService;

	@RequestMapping("/reservationSearch.html")
	public ModelAndView reservationDetail() {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			ModelAndView mav = new ModelAndView("redirect:bedbreakfast-webapp/spring_security_login");
			return mav;
		}
		
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		for (GrantedAuthority authority : user.getAuthorities())
		if(("ROLE_VIEW_ALL_RESERVATION").equals(authority.getAuthority())){
			ModelAndView mav = new ModelAndView("reservationSearchAdmin");
			return mav;
		}
		
		ModelAndView mav = new ModelAndView("reservationSearch");
		return mav;
	}

	@RequestMapping("/findReservationByNumber.html")
	public ModelAndView reservationSearch(@RequestParam int reservationNumber) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);
		List<Reservation> reservationList = new ArrayList<Reservation>();
		reservationList = reservationService.findByNumber(reservationNumber);

		if (reservationList.isEmpty()) {
			return new ModelAndView("reservationSearchNoResults");
		}

		else {
			ModelAndView mav = new ModelAndView("reservationSearchResults");
			mav.addObject("reservationList", reservationList);
			mav.addObject("customer", customer);
			return mav;
		}
	}

	@RequestMapping("/findReservationByRoom.html")
	public ModelAndView findReservationByRoom(@RequestParam int roomNumber) {

		List<Reservation> reservationList = reservationService.findReservationByRoom(roomNumber);
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);

		if (reservationList.isEmpty()) {
			return new ModelAndView("reservationSearchNoResults");
		}

		else {
			ModelAndView mav = new ModelAndView("reservationSearchResults");
			mav.addObject("reservationList", reservationList);
			mav.addObject("customer", customer);
			return mav;
		}
	}
	
	@RequestMapping("/findReservationByStartDate.html")
	public ModelAndView findReservationByStartDate(@RequestParam Date checkIn) {
		
		List<Reservation> reservationList = reservationService.findReservationByStartDate(checkIn);
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);

		if (reservationList.isEmpty()) {
			return new ModelAndView("reservationSearchNoResults");
		}

		else {

			ModelAndView mav = new ModelAndView("reservationSearchResults");
			mav.addObject("reservationList", reservationList);
			mav.addObject("customer", customer);
			return mav;
		}
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}

	public void setReservationService(ReservationService reservationService) {
		this.reservationService = reservationService;
	}

}
